//
//  PocoFeed.m
//  Buzz API Demo
//
//  Created by Peter Hopkins on 5/13/10.
//  Copyright 2010 Google Inc. All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import "PocoFeed.h"

#import "PocoConstants.h"
#import "PocoEntry.h"

@implementation PocoFeed

- (Class)classForEntries {
  return [PocoEntry class];
}


- (void)setupFromXMLElement:(NSXMLElement *)root {
  // we'll parse the generator manually rather than declare it to be an
  // extension so that it won't be compared in isEquals: for the feed
  [self setGenerator:[self objectForChildOfElement:root
                                     qualifiedName:@"generator"
                                      namespaceURI:kGDataNamespaceAtom
                                       objectClass:[GDataGenerator class]]];

  // call subclasses to set up their feed ivars
  [self initFeedWithXMLElement:root];
  
  // allocate individual entries
  Class entryClass = [self classForEntries];
  
  GDATA_DEBUG_ASSERT([[root localName] isEqual:@"response"],
                     @"initing a feed from a non-response element (%@)", [root name]);

  // create entries of the proper class from each "entry" element
  id entryObj = [self objectOrArrayForChildrenOfElement:root
                                          qualifiedName:@"entry"
                                           namespaceURI:kPocoNamespace
                                            objectClass:entryClass];
  if ([entryObj isKindOfClass:[NSArray class]]) {
    
    // save the array
    [self setEntries:entryObj];
    
  } else if (entryObj != nil) {
    
    // save the object into an array
    [self addEntry:entryObj];
  }
}

@end
